package cn.jinjimi.app.custom.ui.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.view.ViewGroup;

import cn.jinjimi.base.util.StringUtils;
import cn.jinjimi.app.R;
import cn.jinjimi.app.bean.Constants;
import cn.jinjimi.app.entity.PhotoInfo;
import cn.jinjimi.app.entity.PictureShow;
import cn.jinjimi.app.managers.OpenActivityManager;
import cn.jinjimi.app.main.adapter.extend.MyBaseAdapter;
import cn.jinjimi.app.main.widget.FrescoImageView.FrescoImageView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by libilang
 * Date: 15/11/5
 * modify by roy 15/11/10
 */
public class CustomInfoPhotoEditAdapter extends MyBaseAdapter<PhotoInfo> {

    public final static int TYPE_NORMAL = 0; // 正常显示的照片
    public final static int TYPE_TAKE_PHOTO = 1; // 拍照按钮
    public final static int TYPE_COUNT = 2;
    private Context mContext;

    public CustomInfoPhotoEditAdapter(Context context) {
        mContext = context;
    }

    public void removeItem(int position) {
        if (getItems() != null) {
            getItems().remove(position);
        }
        notifyDataSetChanged();
    }

    /**
     * 删除
     *
     * @param items
     */
    public void removes(List<PhotoInfo> items) {
        if (getItems() != null) {
            getItems().removeAll(items);
        }
        notifyDataSetChanged();
    }


    @Override
    public int getViewTypeCount() {
        return TYPE_COUNT;
    }

    @Override
    public int getItemViewType(int position) {
        if (position == getItems().size() && getItems().size() < Constants.MAX_VALUE_PHOTOS) {
            return TYPE_TAKE_PHOTO;
        } else
            return TYPE_NORMAL;
    }

    @Override
    public int getCount() {
        if (getItems().size() == Constants.MAX_VALUE_PHOTOS)
            return getItems().size();
        else
            return getItems() != null ? getItems().size() + 1 : 1;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final PhotoInfo photoInfo = getItem(position);

        final PhotoHolder holder;
        if (convertView == null) {
            if (getItemViewType(position) == TYPE_NORMAL) {
                convertView = View.inflate(mContext, R.layout.item_custom_info_photo_pre, null);
            } else {
                convertView = View.inflate(mContext, R.layout.item_custom_info_photo_add, null);
            }
            holder = new PhotoHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (PhotoHolder) convertView.getTag();
        }

        if (getItemViewType(position) == TYPE_NORMAL) {
            if (photoInfo != null) {
                if (StringUtils.isEmpty(photoInfo.getPathFile())) {
                    holder.image.loadView(photoInfo.getThumbnail_url());
                } else {
                    holder.image.loadLocalImage(photoInfo.getAbsolutePath());
                }
                holder.image.setCornerRadius(mContext.getResources().getDimensionPixelOffset(R.dimen.dp_5), Color.WHITE);
            }
            holder.image.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    List<PictureShow> pictureShows = PictureShow.createPhotoPreviewShows(getItems(), holder.image);
                    OpenActivityManager.getInstance().startPhotoPreViewActivity(mContext, pictureShows, position);

                }
            });
        }

        return convertView;
    }

    public class PhotoHolder {
        public FrescoImageView image;

        public PhotoHolder(View view) {
            image = (FrescoImageView) view.findViewById(R.id.item_image);
        }
    }

    public List<PhotoInfo> getUploadItems() {
        List<PhotoInfo> uploadPhotos = new ArrayList<>();
        for (PhotoInfo photoInfo : getItems()) {
            if (StringUtils.isNotEmpty(photoInfo.getPathFile())) {
                uploadPhotos.add(photoInfo);
            }
        }
        return uploadPhotos;
    }
}